package org.jeecg.modules.fund.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.fund.entity.FundLibrary;
import org.jeecg.modules.fund.entity.StockLibrary;
import org.jeecg.modules.fund.mapper.StockLibraryMapper;
import org.jeecg.modules.fund.service.IStockLibraryService;
import org.jeecg.modules.fund.util.StockCodeUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class StockLibraryServiceImpl extends ServiceImpl<StockLibraryMapper, StockLibrary> implements IStockLibraryService {
    @Override
    public void create() {
        // 获取远程的数据
        JSONArray dataList = StockCodeUtil.getHttpData();
        if(dataList != null && !dataList.isEmpty()){
            List<StockLibrary> libraryList = new ArrayList<>();
            for (Object item : dataList){
                JSONObject data = JSONObject.parseObject(item.toString());
                StockLibrary stockLibrary = new StockLibrary()
                        .setId(data.getString("code"))
                        .setCode(data.getString("code"))
                        .setName(data.getString("name"));
                libraryList.add(stockLibrary);
            }
            // 批量更新
            this.saveOrUpdateBatch(libraryList);
        }
    }
}
